#include "resizerect.h"

#include "visbasewidget.h"

ResizeRect::ResizeRect(VisBaseWidget *p) :
    QGraphicsWidget(p)
{
    setAcceptsHoverEvents(true);
    hovering = false;

    setCursor( Qt::SizeFDiagCursor );//make sure arrow cursor is shown in general

    parent = p;
}


void ResizeRect::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    hovering = true;
}


void ResizeRect::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    hovering = false;
}

void ResizeRect::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    mousePressed = true;
}

void ResizeRect::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if( mousePressed )
    {
        float diffX = 100;//event->pos().x()-event->lastPos().x();
        float diffY = 100;//event->pos().y()-event->lastPos().y();
        qDebug()<< "click and drag " << (diffX) << " "  << ( diffY )<< endl;
        parent->resize( 500.0, 500.0 );//parentWidget()->boundingRect().width()+diffX, parentWidget()->boundingRect().height()+diffY  );
    }
}

void ResizeRect::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    mousePressed = false;
}



QRectF ResizeRect::boundingRect() const
{
    float perc = .05;
    return QRectF(-1.0*parentItem()->boundingRect().width()*perc, -1.0*parentItem()->boundingRect().height()*perc, parentItem()->boundingRect().width()*perc, parentItem()->boundingRect().height()*perc);
}

QPainterPath ResizeRect::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void ResizeRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor background(100, 100, 100, 200 );
    painter->setBrush(background);
    //painter->setPen( Qt::NoPen );

    QRectF bRect = boundingRect();
    if( hovering )
    {
        painter->drawRect( bRect );
    }
}
